package com.zsrt.java.day20161227.hzj;

import java.util.Arrays;

import javax.swing.plaf.synth.SynthSeparatorUI;

public class ArrayUtil {

	public static String[] infos=new String[1];
	/**
	 * 写的方法放这里
	 * 
	 */
	public static int add(String name){
		
		int in=0;
		int len = infos.length;
		for(int i=0;i<len;i++){
			
			if(infos[i]!=null){
				if(infos[i].equals(name)){
				System.out.println("此人已经存在，无需要再添加");
				return -1;
				}
			}else if(infos[i]==null){
				infos[i]=name;
				return i;
			}
		}
		String[] newInfos = new String[len+1];
		for(int index =0;index<len;index++){
			String value = infos[index];
			newInfos[index] = value;
		}
		newInfos[len] =name;
		infos = newInfos;
		return len;
	}
	
	public static int del(String name){
		int len =infos.length;
		boolean bol = true;
		String[] newArray;
		if(len!=1){
		newArray = new String[len-1];
		}else{
		newArray = new String[len];	
		}
	    int d=-1;
		for(int index =0;index<len;index++){
			
			if(!infos[index].equals(name) && bol && index<len-1){
			String value = infos[index];
			newArray[index] = value;
			}else if(len==1){		
				newArray[index] =infos[index];
				bol =false;
				if(d==-1){
					d=index;
				}
			}
			else if(index<len-1){
				newArray[index] =infos[index+1];
				bol =false;
				if(d==-1){
					d=index;
				}
			}else if(0==len||index==len-1 && bol){
				return -1;
			}
			
		}
		infos = newArray;
		return d;
	}
	public static int alter(String name,String name1){
		int len =infos.length;
		for(int index =0;index<len;index++){
			
			if(infos[index].equals(name)){
			infos[index]=name1;
			return index;
			}
		}
		return -1;
	}
	
	public static int search(String name){
		int len = infos.length;
		for(int i=0;i<len;i++){
			
			if(infos[i].equals(name)){
				return i;
			}
		}
		return -1;
	}
}
